package com.oauth.global.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.beans.OAuthUrl;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.global.client.beans.ApplicationClientBean;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;

public class ApplicationPanel extends Composite {

	private static ApplicationPanelUiBinder uiBinder = GWT.create(ApplicationPanelUiBinder.class);

	private GlobalRemoteServiceAsync mApplicationAddService;

	private ApplicationClientBean mApplicationClientBean;

	interface ApplicationPanelUiBinder extends UiBinder<Widget, ApplicationPanel> {
	}

	@UiField
	Button addButton;

	@UiField
	SimplePanel imagePanel;

	Image appImage;

	public ApplicationPanel() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setService(GlobalRemoteServiceAsync pApplicationAddRemoteServiceAsync) {
		mApplicationAddService = pApplicationAddRemoteServiceAsync;
	}

	public void setApplicationBean(ApplicationClientBean pApplicationClientBean, Image image) {
		mApplicationClientBean = pApplicationClientBean;
		appImage = image;
		appImage.setSize("200px", "100px");
		imagePanel.add(appImage);
	}

	@UiHandler("addButton")
	void onClick(ClickEvent e) {
		mApplicationAddService.getSignInUrl(mApplicationClientBean.getId(),
				new OAuthAsnyCallBack<OAuthUrl>() {

					@Override
					public void onSuccess(OAuthUrl result) {
						if (result.getOAuthUrl() != null) {
							Window.Location.assign(result.getOAuthUrl());
						}
					}
				});
	}
}
